home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / sw / comm.c++ next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.3 KB  |  117 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <string.h>
  18. #include "comm.h"
  19. #include <unistd.h>
  20. extern "C" {
  21. #include <sys/ioctl.h>
  22. }
  23. #include <sys/socket.h>
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <bstring.h>
  27. #include <netdir.h>
  28.  
  29. //
  30. // NetId
  31. //
  32.  
  33. NetId::NetId()
  34. {
  35.   id.s_addr = INADDR_ANY;
  36. }
  37.  
  38. NetId::NetId(const char* a)
  39. {
  40.   memcpy(&id.s_addr, a, sizeof(id));
  41. }
  42.  
  43. NetId::NetId(InAddr& a)
  44. {
  45.   id = a;
  46. }
  47.  
  48. NetId::NetId(InAddr* a)
  49. {
  50.   id = *a;
  51. }
  52.  
  53. NetId::operator InAddr () const
  54. {
  55.   return id;
  56. }
  57.  
  58. int            NetId::operator == (const NetId& n) const
  59. {
  60.   if (isAny()) return TRUE;
  61.   return (memcmp(&id, &n.id, sizeof(id)) == 0);
  62. }
  63.  
  64. int            NetId::operator != (const NetId& n) const
  65. {
  66.   if (isAny()) return FALSE;
  67.   return (memcmp(&id, &n.id, sizeof(id)) != 0);
  68. }
  69.  
  70. int            NetId::isAny() const
  71. {
  72.   return (id.s_addr == INADDR_ANY);
  73. }
  74.  
  75. //
  76. // Broadcast socket stuff
  77. //
  78.  
  79. int            openBroadcastSocket(int port, struct sockaddr_in* addr)
  80. {
  81.   // Open the socket
  82.   int fd = socket(AF_INET, SOCK_DGRAM, 0);
  83.   if (fd < 0) {
  84.     perror("making broadcast socket");
  85.     return -1;
  86.   }
  87.  
  88.   // bind it to the desired port
  89.   bzero(addr, sizeof(*addr));
  90.   addr->sin_addr.s_addr = INADDR_ANY;
  91.   addr->sin_family = AF_INET;
  92.   addr->sin_port = port;
  93.   if (bind(fd, addr, sizeof(*addr)) < 0) {
  94.     perror("bind");
  95.     close(fd);
  96.     return -1;
  97.   }
  98.  
  99.   // make it a broadcast socket
  100.   int on = 1;
  101.   if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
  102.     perror("setsockopt");
  103.     close(fd);
  104.     return -1;
  105.   }
  106.   addr->sin_addr.s_addr = INADDR_BROADCAST;
  107.  
  108.   // make it non-blocking
  109.   if (ioctl(fd, FIONBIO, &on) < 0) {
  110.     perror("ioctl");
  111.     close(fd);
  112.     return -1;
  113.   }
  114.     
  115.   return fd;
  116. }
  117.